var download = require(PATH+'/libs/download')
var cheerio = require("cheerio")
var crypt = require(PATH+'/libs/crypt')
var fs = require('fs')

exports.index = function(req,res){
	var _url = 'http://www.bdzhe.com/'
	var cachename = PATH+'/caches/index.json'
	fs.exists(cachename,function(exists){
		if(exists){
			var stat = fs.statSync(cachename)
			var mtime = new Date(stat.mtime).getTime()
			var now = new Date().getTime()
			if(now - mtime < 24*60*60*1000){
				fs.readFile(cachename,function(err,data){
					if(err)	console.log(err)
					var lists = JSON.parse(data)
					res.render('index',{
						items:lists
					})
				})
			}else{
				down()
			}
		}else{
			down()
		}
	})
	function down(){
		download(_url,'UTF-8',function(data){
			var items = {
				lists:[],
				titles:[]
			}
			var $ = cheerio.load(data.toString())
			$('.c-box .title h2').each(function(){
				items.titles.push($(this).text())
			})
			var lis = $('.plist li')
			lis.each(function(){
				var li = $(this)
				var src = get_src(li.find('.pic img'))
				items.lists.push({
					name:li.find('.pic').attr('title'),
					tagname:li.find('.set').text(),
					src:src,
					href:crypt.en(li.find('.pic').attr('href'))
				})
			})
			fs.writeFile(cachename, JSON.stringify(items),function(err){
				res.render('index',{
					items:items
				})
			})
		})
	}
}

exports.movie = function(req,res){
	if(!req.params.url)	res.redirect('back')
	var _url = crypt.de(req.params.url)
	var bdzhe = 'http://www.bdzhe.com'
	var url = _url.indexOf(bdzhe) !== -1 ? _url: bdzhe+_url
	download(url,'UTF-8',function(data){
		var $ = cheerio.load(data.toString())
		var nums = []
		$('#liebiao li').each(function(){
			var li = $(this)
			var href = li.find('a').attr('href')
			var is = false
			if(href.indexOf('_') == -1){
				var arr = url.split('/')
				var _href = ''
				for(var i=0;i<arr.length;i++){
					if(i!=arr.length-1){
						_href += arr[i]+'/'
					}
				}
				href = _href+href
			}
			if(href.indexOf(bdzhe) == -1)	href = bdzhe+href
			if(href == url) is = true
			nums.push({
				name:$(this).find('a').text(),
				href:crypt.en(href),
				is:is
			})
		})
		res.render('movie',{
			name:$('.title .c a').text(),
			detail:$('.detail').html(),
			intro:$('.intro').html(),
			msrc:$('#mainFrame').attr('src'),
			nums:nums,
			whref:req.params.url
		})
	})
}	

exports.cate = function(req,res){
	if(!req.params.url)	res.redirect('back')
	var _url = crypt.de(req.params.url)
	var urls = _url.split('/')
	var fname = urls[urls.length-2]
	var cachename = PATH+'/caches/'+fname+'.json'

	fs.exists(cachename,function(exists){
		if(exists){
			var stat = fs.statSync(cachename)
			var mtime = new Date(stat.mtime).getTime()
			var now = new Date().getTime()
			if(now - mtime < 24*60*60*1000){
				fs.readFile(cachename,function(err,data){
					if(err)	console.log(err)
					var lists = JSON.parse(data)
					res.render('cate',{
						items:lists
					})
				})
			}else{
				down()
			}
		}else{
			down()
		}
	})
	function down(){
		download(_url,'UTF-8',function(data){
			var lists = {}
			var childs = []
			var $ = cheerio.load(data.toString())
			var _this = $('#content>.c-box')
			lists.title = _this.find('.title h2').text()
			var lis = _this.find('.plist li')
			lis.each(function(){
				var _this = $(this)
				childs.push({
					name:_this.find('.pic').attr('title'),
					tagname:_this.find('.set').text(),
					src:get_src(_this.find('.pic img')),
					href:crypt.en(_this.find('.pic').attr('href'))
				})
			})
			lists.childs = childs
			fs.writeFile(cachename, JSON.stringify(lists),function(err){
				res.render('cate',{
					items:lists
				})
			})
		})
	}
}

exports.search = function(req,res){
	var qs = require('querystring')
	if(!req.query.keywords)	res.redirect('back')
	var keywords = req.query.keywords
	var _url = 'http://www.bdzhe.com/search.php?keywords='+qs.escape(keywords)
	download(_url,'UTF-8',function(data){
		var $ = cheerio.load(data.toString())
		var lists = []
		var lis = $('#content>.c-box .plist li')
		lis.each(function(){
			var _this = $(this)
			lists.push({
				name:_this.find('.pic').attr('title'),
				tagname:_this.find('.set').text(),
				src:get_src(_this.find('.pic img')),
				href:crypt.en(_this.find('.pic').attr('href'))
			})
		})
		res.render('search',{
			items:lists,
			keywords:keywords
		})
	})
}	

exports.weixin_new = function(req,res){
	var _url = 'http://www.bdzhe.com/'
	download(_url,'UTF-8',function(data){
		var items = []
		var $ = cheerio.load(data.toString())
		var lis = $('.plist li')
		lis.each(function(){
			var li = $(this)
			items.push({
				Title:li.find('.pic').attr('title'),
				PicUrl:li.find('.pic img').attr('src'),
				Url:HOST+'movie/'+crypt.en(li.find('.pic').attr('href'))
			})
		})
		res.json(items)
	})
}

exports.weixin_search = function(req,res){
	var qs = require('querystring')
	var keywords = req.params.keywords
	var _url = 'http://www.bdzhe.com/search.php?keywords='+qs.escape(keywords)
	download(_url,'UTF-8',function(data){
		var $ = cheerio.load(data.toString())
		var datas = []
		var lis = $('#content>.c-box .plist li')
		lis.each(function(){
			var _this = $(this)
			datas.push({
				Title:_this.find('.pic').attr('title'),
				PicUrl:get_src(_this.find('.pic img')),
				Url:HOST+'movie/'+crypt.en(_this.find('.pic').attr('href'))
			})
		})
		res.json(datas)
	})
}

function get_src(img){
	var src = img.attr('src') ? img.attr('src') : img.attr('data-original')
	if(src.indexOf('http') == -1)	src = 'http://www.bdzhe.com'+src
	return src
}